<template>
  <div>
    <n-modal
      v-if="props.showModal"
      v-model:show="props.showModal"
      :mask-closable="true"
      @update:show="$emit('CheckShowModal', false)"
    >
      <n-card
        style="width: 600px"
        title="添加子公司"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
      >
        <template #header-extra>
          <n-button type="error" @click="$emit('CheckShowModal', false)">
            X
          </n-button>
        </template>
        <n-form ref="formRef" :model="model" :rules="rules">
          <n-form-item path="scName" label="公司名称">
            <n-input
              v-model:value="model.scName"
              placeholder="请输入公司名称"
            />
          </n-form-item>
          <n-form-item path="scMan" label="负责人">
            <n-input v-model:value="model.scMan" placeholder="请输入负责人" />
          </n-form-item>
          <n-form-item path="scTel" label="负责人电话">
            <n-input v-model:value="model.scTel" placeholder="请输入号码" />
          </n-form-item>
          <n-form-item path="scWebUrl" label="公司网址">
            <n-input v-model:value="model.scWebUrl" placeholder="请输入网址" />
          </n-form-item>
          <n-form-item path="uId" label="公司管理员">
            <n-input-group>
              <n-select
                :style="{ width: '100%' }"
                :options="Options"
                placeholder="请选择管理员"
                @change="handleChangeRlous"
              />
            </n-input-group>
          </n-form-item>
          <UpLoad @filename="filename" @fileself="fileself"></UpLoad>
          <n-row :gutter="[0, 24]">
            <n-col :span="24">
              <div style="display: flex; justify-content: center">
                <n-button round type="primary" @click="UpdateUserFile">
                  添加
                </n-button>
              </div>
            </n-col>
          </n-row>
        </n-form>
      </n-card>
    </n-modal>
    <n-skeleton v-else text :repeat="2" />
    <n-skeleton text style="width: 60%" />
  </div>
</template>

<script lang="ts" setup>
import { defineProps, ref, reactive } from "vue";
import { addChildren } from "@/api/children";
import { getUser } from "@/api/user";
import UpLoad from "@/components/upload/UpLoad.vue";
const formRef = ref();
const Options: any = ref([]);
const emit = defineEmits(["CheckShowModal", "reloadpage"]);
const props = defineProps({
  showModal: {
    type: Boolean,
    default: false,
  },
});
const model = reactive({
  scName: "",
  scMan: "",
  scTel: "",
  scWebUrl: "",
  uId: "",
  file: "",
});
const rules = {
  scName: [
    {
      required: true,
      message: "请输入公司名称",
      trigger: ["input", "blur"],
    },
  ],
  scMan: [
    {
      required: true,
      message: "请输入负责人",
      trigger: ["input", "blur"],
    },
  ],
  scTel: [
    {
      required: true,
      message: "请输入号码",
      trigger: ["input", "blur"],
    },
    ],
  scWebUrl: [
    {
      required: true,
      message: "请输入网址",
      trigger: ["input", "blur"],
    },
  ],
};
const filename = (val: any) => {
    
};
const fileself = (val: any) => {
    model.file = val;
    // let str = val.name.split(".")
    // model.file = `${model.file}.${str[str.length - 1]}`
    
}
const uId = reactive({})
getUser(uId).then((res) => {
  res.data.data.forEach((item: any) => {
    Options.value.push({
      label: item.uName,
      value: item.uId,
    });
  });
});
const handleChangeRlous = (value:any) => {
  model.uId=value
}
const UpdateUserFile = (e: Event) => {
  e.preventDefault();
  formRef.value.validate((err: any) => {
    if (err) {
      console.log(err);
    } else {
      addChildren(model).then((res: any) => {
        emit("CheckShowModal", false);
        emit("reloadpage");
      });
    }
  });
};
</script>

<style scoped lang="scss"></style>
